package com.cvs.contorller;

import com.cvs.bean.*;
import com.cvs.service.*;
import org.aspectj.lang.annotation.After;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class Usercontroller {
    @Autowired
    private UserService userService;
    @Autowired
    private LevelService levelService;
    @Autowired
    private SignService signService;
    @Autowired
    private JifenService jifenService;
    @Autowired
    private ActivityService activityService;
    @RequestMapping("/home")
    public String home(int uid, Model model){
       List<Level> levels=levelService.findAll();
String levelname="";
       User user= userService.findOne(uid);
        for (Level le:levels) {
            if(user.getJingyan() >=le.getMoney()){
                levelname=le.getName();
            }

        }
        model.addAttribute("levelname",levelname);
      model.addAttribute("user",user);
      model.addAttribute("levels",levels);
        return "home";
    }
    @RequestMapping("/actlog")
    public @ResponseBody Map actlog(int uid){
        Map map=new HashMap();
        map.put("status",200);
        map.put("msg","查询成功");
        map.put("data",jifenService.findCount(uid));
        return map;
    }

    @RequestMapping("/sign")
    public String sign(int uid, Model model){
        model.addAttribute("uid",uid);
        List<Activity> activities=activityService.findAll();
        model.addAttribute("activityies",activities);
        return "sign";
    }


    @PostMapping("/sign")
    public @ResponseBody String sign1(int uid, Model model){
        //Date
        Date date=new Date();
        SimpleDateFormat simpleDateFormat= new SimpleDateFormat("dd");

       String day= simpleDateFormat.format(date);
       System.out.println(day);
        Sign sign=new Sign();
        sign.setCreatetime(date);
        sign.setDay(Integer.parseInt(day));
        sign.setUid(uid);
        int count=signService.addSign(sign);
        System.out.println(count);
        return count+"";
    }
    @PostMapping("/dayList")
    public @ResponseBody
    Map dayList(int uid){

       Map map=new HashMap();
       map.put("status",200);
       map.put("msg","查询成功");
        map.put("data",signService.signDay(uid));
        return map;
    }
    @RequestMapping ("/dayCount")
    public @ResponseBody Map dayCount(int uid){
        Map map=new HashMap();
        map.put("status",200);
        map.put("msg","查询成功");
        map.put("data",signService.signCount(uid));
        return map;
    }
    @PostMapping("/lqjifen")
    public @ResponseBody Map lqjifen(int uid,int actid){
        Activity activity=activityService.findOne(actid);
        User user=userService.findOne(uid);
        user.setJifen(user.getJifen()+activity.getMoney());
        int flag=userService.updateUser(user);
        Jifen jifen=new Jifen();
        jifen.setActivity(activity);
        jifen.setMoney(activity.getMoney());
        jifen.setUid(uid);
        jifen.setCreatetime(new Date());
        jifenService.addJifenlog(jifen);
        Map map=new HashMap();
        if(flag==1){
            map.put("status",200);
            map.put("msg","领取成功");
            map.put("data",null);
        }else{
            map.put("status",200);
            map.put("msg","领取失败");
            map.put("data",null);
        }
        return map;
    }
    @RequestMapping("/jifenlog")
    public String jifenlog(int uid, Model model){
        User user=userService.findOne(uid);
        model.addAttribute("user",user);
        model.addAttribute("list",jifenService.findAll(uid));
        return "jifenlog";
    }

}
